import Vue from 'vue'
import VueRouter from 'vue-router'
import routes from './routes'
import NProgress from 'nprogress'
import 'nprogress/nprogress.css'  //导入进度条样式

Vue.use(VueRouter)

const router = new VueRouter({
  mode: 'history',
  base: process.env.BASE_URL,
  routes
})

//全局前置守卫函数
router.beforeEach((to, from, next) => {

  NProgress.start()

  document.title = to.meta && to.meta.title || '饿了么' 

  var token = localStorage.getItem('token')

  if (to.path == '/index/order' || to.path == '/index/mine') {
    if (token) {
      next()
    } else {
      router.push('/login')
    }
  } else {
    next()
  }
})

//全局后置守卫函数
router.afterEach((to, from) => {
  NProgress.done()
})


export default router
